import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  childHomeList: {},
  currentChildHome: {},
  searchChildHomeCondition: {
    pageSize: 5
  }
};

const getters = {
  childHomeList: state => state.childHomeList,
  currentChildHome: state => state.currentChildHome,
  searchChildHomeCondition: state => state.searchChildHomeCondition
};

const actions = {
  getChildHomePageS({ commit }, param) {
    return Api.getChildHomePage(param).then(ret => {
      commit(types.UPDATE_CHILDHOME_LIST, ret.data.data);
      return ret;
    });
  },
  getChildHomeS({ commit }, id) {
    return Api.getChildHome({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_CHILDHOME, ret.data.data);
      return ret;
    });
  },
  addOrUpdateChildHomeS({ commit }, payload) {
    return Api.addOrUpdateChildHome({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteChildHomeS({ commit }, param) {
    return Api.deleteChildHome(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_CHILDHOME](state, payload) {
    state.currentChildHome = {
      content: "",
      image: "",
      summary: "",
      title: "",
      childHomeId: ""
    };
  },
  [types.UPDATE_CURRENT_CHILDHOME](state, payload) {
    state.currentChildHome = payload;
  },
  [types.UPDATE_CHILDHOME_LIST](state, payload) {
    state.childHomeList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
